from thirdapi.crtsh import CrtSh
from thirdapi.virustotal import VirustotalScan
from dnsx.dnsx import DNSXScan
from config.settings import PACKAGE_PATH
from utils.excel import sheet_compine
import asyncio
import pandas as pd
import openpyxl

xlsx = pd.read_excel('res/2021-08-03-联通在线信息科技有限公司.xlsx', sheet_name='domain-mswo.cn')


def get_subdomains(query: str):
    """query: 域名 """
    dnsx_exec_path = PACKAGE_PATH / 'thirdparty/dnsx'
    crt = CrtSh()
    crt.scan(query)
    vt = VirustotalScan()
    vt.scan(query)
    results = list(crt.results.union(vt.results))
    dnsx = DNSXScan(dnsx_exec_path, results, query=query)
    asyncio.run(dnsx.scan())
    # mswo
    return dnsx.results

dnsx_result = get_subdomains('mswo.cn')

sheet_compine(pd.DataFrame.from_dict(dnsx_result), 'res/2021-08-03-联通在线信息科技有限公司.xlsx', 'domain-mswo.cn', subset=['子域名', '解析结果'])


# print(xlsx)
# result = xlsx
# df2 = pd.DataFrame.from_dict(get_subdomains('mswo.cn'))
# print(df2)
# result = pd.concat([df2, xlsx], ignore_index=True)
# print(result)
# result = result.drop_duplicates(subset=['子域名', '解析结果'],ignore_index=True, keep='first')
# print(result)

# book = openpyxl.load_workbook('res/2021-08-03-联通在线信息科技有限公司.xlsx')
# std= book.get_sheet_by_name('domain-mswo.cn')
# book.remove(std)



# xlsx = pd.ExcelWriter('res/2021-08-03-联通在线信息科技有限公司.xlsx', engine='openpyxl')
# xlsx.book = book
# result.to_excel(xlsx, sheet_name='domain-mswo.cn', index=False)
# xlsx.save()
# xlsx.close()
# print(list(result['子域名']))
